import 'package:flutter/material.dart';
import 'package:ribo_base_core/base_models/base_chart_model.dart';
import 'package:ribo_base_core/base_models/base_chart_radar_model.dart';
import 'package:ribo_base_core/ribo_base_core.dart';
import 'base_chart.dart';

class BaseChartRadar extends StatefulWidget {
  BaseChartRadar({Key key, @required this.model}) : super(key: key);
  final BaseChartRadarModel model;

  @override
  _BaseChartRadarState createState() => _BaseChartRadarState();
}

class _BaseChartRadarState extends State<BaseChartRadar> {
  Map<String, dynamic> _option;

  @override
  Widget build(BuildContext context) {
    _handleModel();

    return BaseChart(
      model: BaseChartModel(style: widget.model?.style, option: this._option),
    );
  }

  void _handleModel() {
    List<BaseChartRadarIndicator> indicator = widget.model?.indicator;
    List<Map<String, dynamic>> indicatorJson =
        indicator.map((e) => e.toJson()).toList();

    this._option = {
      "tooltip": {
        "trigger": 'axis',
        'formatter': widget.model?.tooltipFormatter?.toJson()
      },
      "radar": [
        {
          "indicator": indicatorJson,
          "radius": 60,
          "name": {
            "formatter": BaseChartFormatter.fromFunction("function(value) {return value.split('-')[0] + value.split('-')[1]+'人'}")
          },
          "center": ['50%', '50%'],
        }
      ],
      "series": [
        {
          "type": 'radar',
          'label': {
            "show": false,
            "position": 'top',
            "fontSize": 8,
            'formatter': widget.model?.labelFormatter?.toJson()
          },
          "areaStyle": {},
          "data": widget.model?.data
        }
      ]
    };
  }
}
